<%--
Created by IntelliJ IDEA.
User: 陪你度过漫长岁月
Date: 2022/8/5
Time: 11:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>商品列表</title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>

    <style type="text/css">
        .layui-card-body{
            height: auto;
            white-space: normal;
        }
        .layui-form-checkbox i {
            position: absolute;
            right: 0;
            top: 27px;
            width: 30px;
            height: 28px;
            border: 1px solid #d2d2d2;
            border-left: none;
            border-radius: 0 2px 2px 0;
            color: #fff;
            font-size: 20px;
            text-align: center
        }
    </style>
</head>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="productName" id="productName" placeholder="请输入商品名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-inline" style="width: 100px">
                        <input type="text" name="price" id="priceMin" placeholder="最低价格" class="layui-input" style="width: 100px">
                    </div>
                    <div class="layui-input-inline" style="width: 16px;margin-top: 8px">一</div>
                    <div class="layui-input-inline" style="width: 100px">
                        <input type="text" name="price" id="priceMax" placeholder="最高价格" class="layui-input" style="width: 100px">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">颜色</label>
                    <div class="layui-input-inline">
                        <input type="text" name="color" id="color" placeholder="请输入颜色" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline" style="margin-left: 50px">
<%--                <div class="layui-form-item layui-inline" style="margin-left: 500px">--%>
                    <button class="pear-btn pear-btn-md pear-btn-primary btn1" lay-submit lay-filter="role-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md btn2">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" class="layui-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i
            class="layui-icon layui-icon-vercode"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="role-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="role-enable" {{
           d.enable== true ? 'checked' : '' }} />
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<div align="center">
    <%--    layui 禁用按钮属性 layui-btn-disabled--%>
</div>

<script type="text/html" id="imgtmp">
    <img class="img" src="{{d.mainImage}}" width="50px" height="50px">
</script>
<script type="text/html" id="btn">
<a class="layui-btn layui-btn-danger layui-btn-xs" style="position: relative;top: 10px;border-radius: 10px;line-height: 50px;text-align: center ;background: #ff6700; width: 80px;height: 50px";  lay-event="del">查看详情</a>
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "operate/";

        let cols = [
            [
                {type: 'checkbox', title: '选择', width: 80, style: "", sort: false}
                , {field: 'id', title: '编号', width: 80, sort: true}
                , {field: 'name', title: '商品名称', width: 150, sort: false}
                , {field: 'subtitle', title: '详细信息', width: 135, sort: false}
                , {field: 'mainImage', title: '商品主图',templet:'#imgtmp', width: 150, sort: false}
                , {field: 'price', title: '价格', width: 120, sort: true}
                , {field: 'color', title: '颜色', width: 130, sort: false}
                , {field: 'edition', title: '容量', width: 120, sort: true}
                // , {field: 'capacity', title: '容量', width: 90, sort: true}
                , {field: 'status', title: '商品状态', width: 100,templet:function (data){return data.status==2?"下架":"在售"}, sort: true}
                , {field: '', title: '操作', width: 100,toolbar:"#btn",event: 'stick', sort: true}
            ]
        ]
        console.log(cols)
        table.render({
            elem: '#role-table',
            url: '${pageContext.request.contextPath}/ProduceListServlet',
            page: true,
            cols: cols,
            skin: 'line',
            async: true,
            headers: {
                token: localStorage.token
            },
            request: {
                pageName: 'pageNo',
                limitName: 'pageSize',
            },
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        $('.btn1').on('click',function () {
            table.render({
                elem: '#role-table',
                url: '${pageContext.request.contextPath}/ProduceListServlet?productName='+$('#productName').val()+'&priceMin='+$('#priceMin').val()+'&priceMax='+$('#priceMax').val()+'&color='+$('#color').val(),
                page: true,
                cols: cols,
                skin: 'line',
                async: true,
                headers: {
                    token: localStorage.token
                },
                request: {
                    pageName: 'pageNo',
                    limitName: 'pageSize',
                },
                toolbar: '#role-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
            return false;
        })


        table.on('tool(role-table)', function (obj) {
            $.ajaxSettings.async=false;
            $.getJSON("${pageContext.request.contextPath}/ProduceSelectIdServlet",{id:obj.data.id},function (data){

            },"json")
            layer.open({
                type: 2,
                title: "商品详情",
                closeBtn: 1,
                area: [common.isModile() ? '100%' : '900px', common.isModile() ? '100%' : '900px'], scrollbar: false,
                skin: 'layui-layer-rim my-layer1',
                shadeClose: false,
                content: MODULE_PATH + 'select.jsp?id='+obj.data.id,
                offset: '10px',
                end: function () {
                }
            });

            console.log(obj)
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'power') {
                window.power(obj);
            }
        });

        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(role-query)', function (data) {
            table.reload('role-table', {where: data.field})
            return false;
        });

        form.on('switch(role-enable)', function (obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        // window.add = function () {
        //     layer.open({
        //         type: 2,
        //         title: '新增',
        //         shade: 0.1,
        //         area: ['500px', '400px'],
        //         content: MODULE_PATH + 'add.jsp'
        //     });
        // }
        window.add = function () {
            layer.open({
                type: 2,
                title: "新增",
                closeBtn: 1,
                area: [common.isModile() ? '100%' : '800px', common.isModile() ? '100%' : '800px'], scrollbar: false,
                skin: 'layui-layer-rim my-layer1',
                shadeClose: false,
                content: MODULE_PATH + 'add.jsp',
                offset: '10px',
                end: function () {
                }
            });
        }

        window.power = function (obj) {
            layer.open({
                type: 2,
                title: '授权',
                shade: 0.1,
                area: ['320px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'edit.jsp'
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该角色', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['roleId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].roleId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些用户', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('role-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            table.reload('role-table');
        }
    })
</script>
</body>
</html>
